package com.zhangyu.eat.me.gcfragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhangyu.eat.R;
import com.zhangyu.eat.base.bean.HotBean;
import com.zhangyu.eat.me.adapter.HotAdapter;
import com.zhangyu.eat.me.view.OnRefreshListener;
import com.zhangyu.eat.me.view.RefreshListView;
import com.zhangyu.eat.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotFragment extends Fragment implements OnRefreshListener {


    @BindView(R.id.hot_list)
    RefreshListView hotList;

//    private List<HotBean.ListBean.LivelistBean> mList = new ArrayList<>();

    public HotFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hot, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loadJson("http://api.yiqi1717.com/home/get_multi?nonce=wfeyk7g3tz&pageindex=1&publickey=3.9&tag_id=1&timestamp=1476792143&token=c87ed51849b51c3db4bfad026c33c201&signature=f0b0c3f3d732789b502711f769562c8d");
    }

    public void loadJson(String s) {
        OkHttpUtils.getDataAsync(s, new Callback() {
            private List<HotBean> list = new ArrayList<>();

            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body().string();
                        Gson gson = new Gson();
                        HotBean item = gson.fromJson(json, HotBean.class);
                        Log.d("HotFragment", item.getRet());
                        list.add(item);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        List<HotBean.ListBean.LivelistBean> mList = list.get(0).getList().get(2).getLivelist();
                        Log.d("HotFragment", mList.get(0).getTitle());
                        HotAdapter adapter = new HotAdapter();
                        adapter.setData(mList);
                        hotList.setAdapter(adapter);

                    }
                });
            }
        }, getActivity());

    }

    @Override
    public void onDownPullRefresh() {

    }

    @Override
    public void onLoadingMore() {

    }
}
